package com.myComsole.chat;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author: hfxiang
 * @Description: 专注获取某次对话用户方的发言（必须是每次都深度思考） ds一个对话总字数限制为8万4左右，ds一般是用户发言的3倍这样，不考虑一次会话大小限制的话最极限可以发言到8万字，想办法精简，或者分段让ds总结压缩多余的信息
 * @date: 2025/16/18 16:45
 */
public class dealChatResult {
    public static void main(String[] arg) {
        String sourcePath = "D:\\chatgpt对话\\conversations.json";
        String resultPath = "D:\\chatgpt对话\\result.txt";
        if (FileUtil.exist(resultPath)) {
            FileUtil.del(resultPath);
        }
        File file = new File(sourcePath);
        String text = FileUtil.readString(file, Charset.forName("utf-8"));
        //转换中文字符编码
        String chineseText = UnicodeUtil.toString(text);
        JSONArray jsonArray = JSONUtil.parseArray(chineseText);
        JSONObject lastSession = selectByTitle(jsonArray,"继续对话分析");
        List<String> myContents = selectUserWordList(lastSession);
        String resultText = dealMyContents(myContents);
        FileUtil.writeString(resultText, resultPath, Charset.forName("utf-8"));
    }
    private static String dealMyContents(List<String> myContents){
        StringBuilder content = new StringBuilder();
        for (String myContent : myContents) {
            myContent = myContent.trim().replace("\r\n","。");
            myContent = myContent.trim().replace("\n","。");
            myContent = myContent.trim().replace("\t","。");
            content.append(myContent).append("。");
        }
        return content.append(".").toString();
    }

    private static JSONObject selectByTitle(JSONArray jsonArray,String title){
        for (Object o : jsonArray) {
            JSONObject jsonObject = (JSONObject) o;
            if (jsonObject.getStr("title").contains(title)){
                return jsonObject;
            }
        }
        return null;
    }
    private static List<String> selectUserWordList(JSONObject sessionObject){
        JSONObject mapping = sessionObject.getJSONObject("mapping");
        List<String> myContents = new ArrayList<>();
        Collection<Object> objects = mapping.values();
        for (Object object : objects) {
            JSONObject jsonObject = (JSONObject) object;
            if (jsonObject.get("message")==null) {
                continue;
            }
            JSONObject message = jsonObject.getJSONObject("message");
            //判断角色 非user跳过
            if (message.get("author")==null) {
                continue;
            }
            JSONObject author = message.getJSONObject("author");
            if (author.get("role")==null) {
                continue;
            }
            String role = author.getStr("role");
            if (!StrUtil.equals(role,"user")) {
                continue;
            }
            if (message.get("content")==null) {
                continue;
            }
            JSONObject myContent = message.getJSONObject("content");
            if (myContent.get("parts")==null) {
                continue;
            }
            JSONArray parts = myContent.getJSONArray("parts");
            //获取对话内容
            for (Object part : parts) {
                String word = part.toString();
                myContents.add(word);
            }
        }
        return myContents;
    }

}
